package com.raychi.demo.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.raychi.demo.dao.CustomDao;
import com.raychi.demo.entity.Custom;
import com.raychi.demo.util.JdbcUtil;

public class CustomService {
	private CustomDao customDao;
	

	public List<Custom> getcustoms() throws ClassNotFoundException, SQLException {
		Connection conn = null;
		List<Custom> list = null;
		try {
			conn = JdbcUtil.getConnection();
			list = customDao.getcustoms(conn);
			conn.commit();
		} catch (Exception e) {
			JdbcUtil.rollback(conn);
		} finally {
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	public void delete(Custom custom) throws Exception{
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			customDao.delete(custom, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void update(Custom custom) throws Exception{
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			customDao.update(custom, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void save(Custom custom) throws Exception{
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			customDao.save(custom, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	public void setCustomDao(CustomDao customDao) {
		this.customDao = customDao;
	}
}
